home *** CD-ROM | disk | FTP | other *** search
/ develop, the CD; issue 1 / Apple_Develop_1989.bin / Offscreen / FracApp 2.0B3 / UFracApp.p < prev   
Text File  |  1989-06-29  |  11KB  |  264 lines

  1. { FracAppPalette:
  2.     Copyright 1988 by Bob.  All rights reserved, since Bob has all rights.
  3.     February 1, 1988. 
  4.     Written by Bo3b Johnson of Developer Technical Support. }
  5.  
  6. UNIT UFracApp;
  7.  
  8. (*
  9.  
  10. This is a not too small application which can calculate a fractal in full color
  11.     on the Mac II, using direct 881 code for speed.  It saves files on disk as PICT.
  12.     For full info, see the implementation.   This version specifically is set up to
  13.     be fully compatible, and does not support color table animation.
  14.  
  15. *)
  16.  
  17. INTERFACE
  18.  
  19. USES
  20.     {$LOAD FracApp881.LOAD}
  21.         MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
  22.         UMacAppUtilities, UViewCoords, UFailure,
  23.         UMemory, UMenuSetup, UObject, UList, UAssociation, UMacApp,
  24.         PaletteMgr, UPrinting, OffScreen; { adds new offworld calls }
  25.     {$LOAD}
  26.  
  27. CONST
  28.  
  29.   {Command numbers}
  30.  
  31.     kSignature                = 'Arf ';        {Application signature}
  32.     kFileType                    = 'PICT';    {File-type code used for document files
  33.                                                         created by this application}
  34.     kFracAppWindowID    = 1001;    {This is passed to NewSimpleWindow as the
  35.                                                         resource ID of the the WIND Resource
  36.                                                         which defines the window for this
  37.                                                         application's documents}
  38.     kMultiDialog                = 1000;    { Dialog Id for multipage dialog, gets number of pages to do.}
  39.     kHorItem                    = 3;            { item in dialog for EditText of horizontal page count. }
  40.     kVerItem                    = 4;            { item in dialog for EditText of vertical page count. }
  41.  
  42.     kStaggerAmount        = 30;
  43.     kPICTHeaderSize         = 512;        { 512 bytes off the file are used for our info and print info. }
  44.     kSelPattern                = 128;        { pattern resource Id. }
  45.     kNewFractal             = 1000;    { item Id for New Fractal menu option. }
  46.     kNewMultiPage         = 1001;    { item Id for New Multipage... menu option. }
  47.     kPageId                    = 1001;    { 'page' resource Id. }
  48.     kClut                         = 501;        { Res Id for the clut resource that we use for offscreen. }
  49.     kNumColors                = 195;        { number of colors we animate, and use in calculation. }
  50.     kWrongMachine            = 1000;    { error code if we cannot run, from ForceEnvirons. }
  51.     
  52.     kRectRight                = 608;        { Full page size rectangle for color Tek Printer }
  53.     kRectBottom            = 798;        { Full page size for bottom }
  54.     
  55.     envDontCare             = 0;            { don't care is always 0, for ForceEnvirons. }
  56.  
  57. TYPE
  58.     { The header record for each of the files saved by FracApp.  This header includes the
  59.         pertinent data that allows a fractal to be restarted, as well as displayed
  60.         on the screen.  The print record follows this info in the first 512 bytes of a file,
  61.         but that info is read by the standard DoRead/DoWrite methods.  Some of the fields
  62.         are LongInts to avoid rounding errors during calculations. }
  63.     FracRecord  = RECORD
  64.         fType:        OSType;            { 4  set as 'Arf ' for these documents. }
  65.         hdrId:        Integer;            { 2  as 'FA' FracApp header ID. }
  66.         version:    Integer;              { 2  file version decides type of file. }
  67.         done:        Boolean;            { 2  if the fractal is finished or not. }
  68.         realMin:    Extended;            { 12  minimum value of fractal on real/p axis. }
  69.         realMax:    Extended;            { 12  maximum value of fractal on real/p axis. }
  70.         imagMin:    Extended;            { 12  minimum on imaginary/q axis. }
  71.         imagMax:    Extended;            { 12  maximum on imaginary/q axis. }
  72.         deltaP:        Extended;            { 12  horizontal step size in fractal space. }
  73.         deltaQ:        Extended;            { 12  vertical step size in fractal space. }
  74.         plotWidth: LongInt;            { 4  width of fractal area in pixels. }
  75.         plotHeight: LongInt;            { 4  height of fractal area in pixels. }
  76.         calcRect:    Rect;                { 8  rectangle surrounding the window it was built for. }
  77.         curRow:    LongInt;            { 4  counter for current pixel position to be done.  Vertical. }
  78.         curCol:        LongInt;            { 4  counter for current pixel.  Horizontal. }
  79.         elapsedTime: LongInt;        { 4  amount of time to do this fractal in seconds. }
  80.     END;  { FracRecord. }
  81.  
  82.  
  83.  
  84. {-------------------------------  Application  -------------------------------}
  85.  
  86.     TFracAppApplication = OBJECT(TApplication)
  87.  
  88.         
  89.         PROCEDURE TFracAppApplication.IFracAppApplication (itsMainFileType: OSType);
  90.             {Initializes the application and globals.}
  91.             
  92. (*        PROCEDURE TFracAppApplication.Close;  OVERRIDE;
  93.             { Removes idle coHandler as we exit. }
  94. *)
  95.         FUNCTION  TFracAppApplication.DoMakeDocument(
  96.                 itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  97.             {Launches a TFracAppDocument; called when application's icon is
  98.                 opened, and when New or Open is requested by the user.
  99.                 Every application which uses Documents MUST override this
  100.                 method}
  101.                 
  102. {        PROCEDURE  TFracAppApplication.DoIdle (Phase: IdlePhase); OVERRIDE;
  103.             { Performs Idle time processing for the application.  This will do the
  104.                 fractal calculation during the idle times.  It will allow each open
  105.                 view a chance to calculate. }
  106.  
  107.     END;    { TFracAppApplication }
  108.  
  109.  
  110. {-------------------------------  Document  -------------------------------}
  111.  
  112.     TFracAppDocument = OBJECT(TDocument)
  113.  
  114.         { Now the fields that are special to the fractal itself.  These are the
  115.             variables that make up the display state and the definition of the
  116.             fractal itself.  They are associated with the document so they can
  117.             be stored in a file, and read back in.  Essentially a global state for
  118.             each fractal document. }
  119.  
  120.         fFracHeader:                            FracRecord;            { global state on fractal. }
  121.         fStartTime:                            LongInt;                { starting time of calculations. }
  122.         fBigBuff:                                Ptr;                        { pointer to offscreen data }
  123.         fDrawingDevice:                     GDHandle;                { handle to our offscreen gDevice. }
  124.         fDrawingPort:                        CGrafPtr;                { pointer to drawing buffer. }
  125.  
  126.         fFracAppView:                        TFracAppView;
  127.             {Every document object must preserve references to all the views it
  128.                 creates as fields of the document object, since DoMakeWindows
  129.                 will need to know which views to install in the windows it
  130.                 creates}
  131.  
  132.  
  133.         PROCEDURE  TFracAppDocument.BuildOffWorld (sizeOfDoc: Rect);
  134.             { Allocates offscreen gDevice and port for document data. }
  135.             
  136.         PROCEDURE  TFracAppDocument.SetUpConstants;
  137.             { Sets up starting constants for calculation. }
  138.             
  139.         PROCEDURE      TFracAppDocument.IFracAppDocument;
  140.             {Init routine for the document, sets up the object, then the fractal default state. }
  141.  
  142.         PROCEDURE TFracAppDocument.DoInitialState;  OVERRIDE;
  143.             { Does the work for a New operation, where we start with a new fractal
  144.                 that doesn't have any stored data.  This is set up the view with no
  145.                 data and set up the fractal coordinates to the default. }
  146.  
  147.         PROCEDURE TFracAppDocument.DoMakeWindows; OVERRIDE;
  148.             {Launches the window which will represent the document on the
  149.                 screen.  Every document which has any screen display MUST
  150.                 override this method}
  151.  
  152.         PROCEDURE TFracAppDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;
  153.             {Launches the view which is seen in the document's window.  Every
  154.                 document which has any screen display or which can be printed
  155.                 MUST override this method}
  156.  
  157.         PROCEDURE TFracAppDocument.DoNeedDiskSpace(VAR dataForkBytes, 
  158.                                                 rsrcForkBytes: LONGINT);  OVERRIDE;
  159.             { Finds out the entire size of the object to be saved into the file so that the
  160.                 correct amount of disk space can be used.  }
  161.  
  162.         PROCEDURE TFracAppDocument.DoRead(aRefNum: INTEGER; rsrcExists,
  163.                                                 forPrinting: BOOLEAN);    OVERRIDE;
  164.             { Reads in the data out of the data fork of the file, and stows it into the
  165.                 document's bitMap.  Also resets the vars in the document object to match
  166.                 the saved values from the header. }
  167.  
  168.         PROCEDURE TFracAppDocument.DoWrite(aRefNum: INTEGER;
  169.                                                 makingCopy: BOOLEAN);    OVERRIDE;
  170.             { Converts the data in the document's port into a PICT, then writes that block
  171.                 out to the file, making it into a standard PICT file. }
  172.  
  173.         PROCEDURE TFracAppDocument.FreeData; OVERRIDE;
  174.             { Free method usually used for Rever operation, kept here to show the
  175.                 normal Free approach. }
  176.  
  177.         PROCEDURE TFracAppDocument.Free; OVERRIDE;
  178.             { Free method for our document.  It disposes the data block of the picture
  179.                 data that was read in from the disk, and kills offscreen stuff. }
  180.  
  181.         FUNCTION TFracAppDocument.DoIdle(phase: IdlePhase):  BOOLEAN; OVERRIDE;
  182.             { Idle for the doc to call CalcCity and start up new auto docs if needed. }
  183.             
  184.         FUNCTION  TFracAppDocument.CalcCity:  BOOLEAN;
  185.             { Does the idle time calculations for the document.  This is for